/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servergui;

import convert_crypto.ConvertDatabaseGUI;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 *
 * @author Backkom
 */
public class MenuBarCustom implements ActionListener {

    public JMenuBar menuBar;
    public JMenu about;
    public JMenu file;
    public JMenu database;
    public JMenu convert;
    public JMenu table;
    public JPopupMenu popupFile;
    public JPopupMenu popupDB;
    public JPopupMenu popupTB;
    public JPopupMenu popupTabs;
    private MainScreen parent;

    public MenuBarCustom(MainScreen main) {
        parent = main;
        menuBar = new JMenuBar();
        file = new JMenu("File");
        about = new JMenu("About");
        database = new JMenu("Database");
        database.setEnabled(false);
        convert = new JMenu("Convert");
        table = new JMenu("Table");
        table.setEnabled(false);

        popupFile = new JPopupMenu();
        popupDB = new JPopupMenu();
        popupTB = new JPopupMenu();
        popupTabs = new JPopupMenu();

        JMenuItem close = InitItem("Close", 1);
        JMenuItem aboutTeam = InitItem("AboutTeam", 2);

        JMenuItem newDatabase = InitItem("New Database", 3);
        JMenuItem delDatabase = InitItem("Delete Database", 4);
        JMenuItem newTable = InitItem("New Table", 5);
        JMenuItem delTable = InitItem("Delete Table", 6);
        JMenuItem designTB = InitItem("Design Table", 7);
        JMenuItem viewTB = InitItem("View Table", 8);
        JMenuItem queryDB = InitItem("Query Database", 9);
        JMenuItem convertDB = InitItem("Convert Database", 10);
        file.add(newDatabase);
        file.addSeparator();
        file.add(close);
        database.add(newTable);
        database.add(delDatabase);
        database.add(queryDB);
        table.add(designTB);
        table.add(viewTB);
        table.add(delTable);
        convert.add(convertDB);
        about.add(aboutTeam);

        menuBar.add(file);
        menuBar.add(database);
        menuBar.add(table);
        menuBar.add(convert);
        menuBar.add(about);

        JMenuItem f1 = InitItem("New Database", 3);
        popupFile.add(f1);

        JMenuItem db1 = InitItem("New Table", 5);
        JMenuItem db2 = InitItem("Delete Database", 4);
        JMenuItem db3 = InitItem("Query Database", 9);
        popupDB.add(db1);
        popupDB.add(db2);
        popupDB.add(db3);

        JMenuItem tb1 = InitItem("Design Table", 7);
        JMenuItem tb2 = InitItem("View Table", 8);
        JMenuItem tb3 = InitItem("Delete Table", 6);
        popupTB.add(tb1);
        popupTB.add(tb2);
        popupTB.add(tb3);
        JMenuItem v1 = InitItem("Close", 11);
        popupTabs.add(v1);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int Action = Integer.parseInt(e.getActionCommand());
        switch (Action) {
            case 1: // Close
                parent.Close();
                break;
            case 2: //About
                parent.ShowAbout();
                break;
            case 3: // New Database
            {
                String dbName = JOptionPane.showInputDialog(parent, "Database name:", "New Database", JOptionPane.QUESTION_MESSAGE);
                if (dbName != null && !dbName.isEmpty()) {
                    parent.addNode(dbName);
                }
            }
            break;
            case 4: // Delete Database
                parent.deleteNode();
                break;
            case 5: // New Table
                String tbName = JOptionPane.showInputDialog(parent, "Table name:", "New Database", JOptionPane.QUESTION_MESSAGE);
                if (tbName != null && !tbName.isEmpty()) {
                    parent.addNode(tbName);
                }
                break;
            case 6: // Delete Table
                parent.deleteNode();
                break;
            case 7: // Table Design
                parent.viewDesign();
                break;
            case 8: // View Table
                parent.viewData();
                break;
            case 9: // Query Database
                parent.queryDB();
                break;
            case 10:
                ConvertDatabaseGUI convertGUI = new ConvertDatabaseGUI();
                convertGUI.showGUI();
                break;
            case 11:
                parent.closeTab(-1);
                break;
        }
    }

    private JMenuItem InitItem(String title, int numCommand) {
        JMenuItem item = new JMenuItem(title);
        item.addActionListener(this);
        item.setActionCommand(Integer.toString(numCommand));
        return item;
    }

    synchronized void ShowPopup(MouseEvent e, int i, Point point) {
        int index = i;
        Point p = point;
        switch (index) {
            case 0: // On Tree View
                popupFile.show(e.getComponent(), p.x, p.y);
                break;
            case 1: //On Database            
                popupDB.show(e.getComponent(), p.x, p.y);
                break;
            case 2: //On Table                  
                popupTB.show(e.getComponent(), p.x, p.y);
                break;
        }
    }

    void TreeSelect(int i) {
        if (i == 1) //Select database
        {
            database.setEnabled(true);
            table.setEnabled(false);
        }
        if (i == 2) //Select table
        {
            table.setEnabled(true);
            database.setEnabled(false);
        }
        if (i == 0) {
            table.setEnabled(false);
            database.setEnabled(false);
        }
    }
}
